<?php
/**
 * Created by PhpStorm.
 * User: ChenJulong
 * Date: 2019/8/27
 * Time: 10:28
 */


namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;


class FinancingListModel extends BaseModel
{
    public $table = 'financing_list';

    public $timestamps = false;

    /**
     * 写收益流水日志
     */
    public static function write_profit(int $member_id, float $money, float $balance, string $call, $remark, $discount = 0)
    {
        $table = $member_id % 20;
//        if ($table < 10) $table = '0' . $table;
//        $table = 'financing_list_' . $table;
        $table  = str_pad($table, 2, 0, STR_PAD_LEFT);
        $mold   = self::get_mold($call);
        $sqlmap = [
            'member_id'   => $member_id,
            'money'       => $money,
            'balance'     => $balance,
            'mold_id'     => $mold->id,
            'mold_name'   => $mold->name,
            'remark'      => $remark,
            'create_time' => time(),
            'discount'    => $discount //优惠金
        ];
        return DB::table('financing_profit')->insert($sqlmap);
    }

    /**
     * 写流水日志
     */
    public static function write_log(int $member_id, float $money, float $balance, string $call, $remark, $discount = 0)
    {
        $table = $member_id % 20;
        if ($table < 10) $table = '0' . $table;
        $table = 'financing_list_' . $table;

        $mold   = self::get_mold($call);
        $sqlmap = [
            'member_id'   => $member_id,
            'money'       => $money,
            'balance'     => $balance,
            'mold_id'     => $mold->id,
            'mold_name'   => $mold->name,
            'remark'      => $remark,
            'create_time' => time(),
            'discount'    => $discount //优惠金
        ];
        return DB::table('financing_list')->insert($sqlmap);
    }

    /**
     * 获取流水类型
     */
    public static function get_mold($call)
    {
        Cache::remember('financing_mold_call_' . $call, 6000, function () use ($call) {
            $data = DB::table('financing_mold')->where('call', $call)->first();
            return $data;
        });
    }


}
